//持久存储插件
import createPersistedState from 'vuex-persistedstate'

const namespaced = true;

/**
 * 判断数据是否为空,为空则返回false;
 */
function changeData(name) {
    let obj = localStorage.getItem('checked')
    console.log(obj)
    if (obj == null) {
        return false
    } else {
        return eval(obj.toLowerCase())
    }

}

/**
 * 公共变量
 */
const state = {
    isShowAsideMenu: false,
    loadingShow: false,
    //保存访问接口的口令token
    token: localStorage.getItem('token'),
    //邮箱,用户名,用户密码
    email: localStorage.getItem('email'),
    userId: localStorage.getItem('userId'),
    username: localStorage.getItem('username'),
    userPwd: localStorage.getItem('userPwd'),
    // checked: eval(localStorage.getItem('checked').toLowerCase()),
    checked: changeData('checked'),
    avatar: localStorage.getItem('avatar'),
    superAdmin: localStorage.getItem('superAdmin') == null ? 0 : localStorage.getItem('superAdmin'),
};

const getters = {
    isShowAsideMenu: state => state.isShowAsideMenu,
    loadingShow: state => state.loadingShow,
    token: state => state.token, //token
    userId: state => state.userId, //用户id
    username: state => state.username, //用户名
    email: state => state.email, //邮箱账号
    userPwd: state => state.userPwd, //用户密码
    checked: state => state.checked, //是否保存密码
    avatar: state => state.avatar, //用户头像
    superAdmin: state => state.superAdmin, //是否管理员 0-否 , 1-是
};

const mutations = {
    showAsideMenu(state, flag) {
        state.isShowAsideMenu = flag;
    },
    update_loading(state, data) {
        state.loadingShow = data;
    },
    update_token(state, data) {
        window.localStorage.setItem('token', data);
        state.token = data;
    },
    update_email(state, data) {
        window.localStorage.setItem('email', data);
        state.email = data;
    },
    update_username(state, data) {
        window.localStorage.setItem('username', data);
        state.username = data;
    },
    update_userPwd(state, data) {
        window.localStorage.setItem('userPwd', data);
        state.userPwd = data;
    },
    update_checked(state, data) {
        window.localStorage.setItem('checked', data);
        state.checked = data;
    },
    update_avatar(state, data) {
        window.localStorage.setItem('avatar', data);
        state.avatar = data;
    },
    update_userId(state, data) {
        window.localStorage.setItem('userId', data);
        state.userId = data;
    },
    update_superAdmin(state, data) {
      window.localStorage.setItem('superAdmin', data);
      state.superAdmin = data;
    }
};

const actions = {};

export default {
    namespaced,
    state,
    getters,
    mutations,
    actions
};